package com.ibm.workplace.wcm.cqplugin.ui.preference;

import org.eclipse.swt.*;
import org.eclipse.swt.events.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

import com.ibm.workplace.wcm.cqplugin.*;
import com.ibm.workplace.wcm.cqplugin.cq.CqConnectionProfile;

public class CqConnectionProfileComposite extends Composite implements FocusListener {
    
    private Text nameText;
    private Text familyText;
    private Text hostText;
    private Text portText;
    private Text userNameText;
    private Text passwordText;
    private Text schemaText;

    public CqConnectionProfileComposite(Composite arg0, int arg1) {
        super(arg0, arg1);
        
        GridLayout layout = new GridLayout(2, false);
        layout.marginWidth = 0;
        layout.marginHeight = 0;
        setLayout(layout);

        nameText = createField(this, "Profile name:", "", 100, 0);
        Label label = new Label(this, SWT.SEPARATOR | SWT.HORIZONTAL);
        GridData data = new GridData(GridData.FILL_HORIZONTAL);
        data.horizontalSpan = 2;
        label.setLayoutData(data);
        
        //commented out by Kevin
        //familyText = createField(this, "Family:", "", 100, 0);
        
        hostText = createField(this, "Host:", "", 150, 0);
        portText = createField(this, "Port:", "", 50, 0);
        schemaText = createField(this, "Schema:", "", 150, 0);
        userNameText = createField(this, "User name (not W3 user name):", "", 100, 0);
        passwordText = createField(this, "Password:", "", 100, SWT.PASSWORD);
        
        nameText.setFocus();
    }
        
    private Text createField(Composite parent, String caption, String value, int width, int textStyle) {
        Label label = new Label(parent, 0);
        label.setText(caption);
        
        Text text = new Text(parent, textStyle | SWT.BORDER);
        text.setText(value != null ? value : "");
        if (width != -1) {
            GridData gridData = new GridData();
            gridData.widthHint = width;
            text.setLayoutData(gridData);
        }
        text.addFocusListener(this);       
        
        return text;
    }
        
    public void init(CqConnectionProfile profile) {
        nameText.setText(profile.getName());
        //familyText.setText(profile.getFamily());
        hostText.setText(profile.getHost());
        portText.setText(profile.getPort());
        userNameText.setText(profile.getUserName());
        passwordText.setText(profile.getPassword());
        schemaText.setText(profile.getSchema());
    }
    
    public void store(CqConnectionProfile profile) {
        profile.setName(nameText.getText());
        //profile.setFamily(familyText.getText());
        profile.setHost(hostText.getText());
        profile.setPort(portText.getText());
        profile.setUserName(userNameText.getText());
        profile.setPassword(passwordText.getText());
        profile.setSchema(schemaText.getText());
    }

    public void focusGained(FocusEvent event) {
        if (event.getSource() instanceof Text) {
            ((Text)event.getSource()).selectAll();
        }
    }

    public void focusLost(FocusEvent arg0) {
        // TODO Auto-generated method stub
        
    }

}
